//打开socket连接 参数--ws连接地址，callBack回调函数
var app = getApp();
var socketPath = app.globalData.path;
function openSocket(ws, callBack) {
  wx.connectSocket({ url: ws });
  //小程序判断是否打开websocket的方法
  wx.onSocketOpen(function (res) {
    typeof callBack == "function" && callBack(true);
  })
}

//监听关闭socket
function closeSocket(callBack) {
  wx.onSocketClose(function (res) {
    typeof callBack == "function" && callBack(false);
  })
}
//发送消息
function sendMessage(msg) {
  wx.request({
    url: socketPath+'diTui_aiRadar/websocket/send',
    data:msg,
    success:function(res){
      if (res.statusCode == 200){
        console.log('发送成功')
      }else{
        console.log('发送失败')
      }
    }
  })
  // wx.sendSocketMessage({
  //   data: msg
  // })
}

//接收消息
function receiveMessage(callBack) {
  wx.onSocketMessage(function (res) {
    typeof callBack == "function" && callBack(res.data);
  })
}

module.exports = {
  openSocket: openSocket,
  closeSocket: closeSocket,
  sendMessage: sendMessage,
  receiveMessage: receiveMessage
}